﻿using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using NETCore.Encrypt;


namespace Jst.Common
{
    public static class EncryptExtensions
    {
        private static readonly string key = "TEb32E7uSx5vR4GLQq0rbqHBqU2sTVxF";

        /// <summary>
        /// 加密---返回密文
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string AesEncrypt(this string value)
        {
            return EncryptProvider.AESEncrypt(value, key);
        }

        /// <summary>
        /// 加密---返回密文
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string AesObjEncrypt<T>(this T value)
        {
            var serializerSettings = new JsonSerializerSettings
            {
                // 设置为驼峰命名
                ContractResolver = new CamelCasePropertyNamesContractResolver(),
            };
            var str = JsonConvert.SerializeObject(value, Formatting.Indented, serializerSettings);
            return EncryptProvider.AESEncrypt(str, key);
        }

        /// <summary>
        /// 解密---返回字符串
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string AesStrDecrypt(this string value)
        {
            return EncryptProvider.AESDecrypt(value, key);
        }

        /// <summary>
        /// 解密---返回对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static T AesObjDecrypt<T>(this string value)
        {
            var str = EncryptProvider.AESDecrypt(value, key);
            return JsonConvert.DeserializeObject<T>(str);
        }
    }
}
